<?php 

namespace Admin\Controller;
use Admin\Controller\BaseController;

/**
 * 后台首页控制器
 * @author 艾逗笔<765532665@qq.com>
 */
class IndexController extends BaseController {

	/**
	 * 后台首页
	 * @author 艾逗笔<765532665@qq.com>
	 */
	public function index() {
        $this->assign('meta_title', '后台首页');
		$this->display();
	}
	/**
	 * 列表展示
	 * @author 佛祖<381361943@qq.com>
	 */
	public function Homepagemanagement()
	{
		$list = M('index')->select();
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 单条信息修改
	 * @author 佛祖<381361943@qq.com>
	 */
	public function edit()
	{
		if(IS_GET)
		{
			$id = I('cid',0);
			$id == 0?$this->error('参数错误,id不能为0'):1;
		  	$list = M('index')->where(array('id'=>$id))->find();
		  	$list == ''?$this->error('数据不存在'):1;
		  	$this->assign('list',$list);
		  	$this->display(); 
		}
		else
		{
			$data['order'] = I('post.order',0);
			$id = I('post.id');
			$id == 0?$this->error('参数无效'):1;
			$data['order'] == 0?$this->error('排序必须大于1'):1;
			M('index')->where(array('id'=>$id))->save($data);
			$this->success('修改成功',U('Homepagemanagement'));
		}	
	}
	/**
	 * 轮播图列表
	 * @author 佛祖<381361943@qq.com>
	 */
	public function picture()
	{
		$list = M('picture')->order("id desc")->select();
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 轮播图添加
	 * @author 佛祖<381361943@qq.com>
	 */
	public function pictureadd()
	{
		/*$a = "http://localhost/douxin/Uploads/Pictures/20180725/5b580cdd40a86.jpeg"; 
		$count=strpos($a,"http://localhost"); 
		$str=substr_replace($a,"",$count,strlen('http://localhost'));
		echo $str;die;
		echo get_domain('http://localhost/douxin/Uploads/Pictures/20180725/5b580cdd40a86.jpeg');die;*/
		if(IS_GET)
		{
			$this->display();
		}
		else
		{
			$data = I('post.');
			$count=strpos($data['url'],get_domain($data['url']));
			$data['url']=substr_replace($data['url'],"",$count,strlen(get_domain($data['url'])));
			if(strstr($data['href'],'http://') === false && strstr($data['href'],'https://')===false)
				$this->error('轮播链接请在开头处拼接http:// 或https://');
			M('picture')->add($data);
			$this->success('添加成功',U('picture'));
		}	
	}
	/**
	 * 轮播图删除
	 * @author 佛祖<381361943@qq.com>
	 */
	public function delete()
	{
		$id = I('get.id',0);
		$id == 0?$this->error('参数错误,id不能为0'):1;
		M('picture')->delete($id);
		$this->success('删除成功',U('picture'));
	}
	/**
	 * 轮播图修改
	 * @author 佛祖<381361943@qq.com>
	 */
	public function pictureedit()
	{
		$id = I('id',0);
		if(IS_GET)
		{
			$id = I('get.id',0);
			$id == 0?$this->error('参数错误,id不能为0'):1;
			$list = M('picture')->find($id);
			!$list?$this->error('数据已经被删除'):1;
			$this->assign('list',$list);
			$this->display();
		}
		else
		{
			$data = I('post.');
			unset($data['id']);
			$count=strpos($data['url'],get_domain($data['url']));
			$data['url']=substr_replace($data['url'],"",$count,strlen(get_domain($data['url'])));
			M('picture')->where(array('id'=>$id))->save($data);
			$this->success('修改成功',U('picture'));
		}	
	}
	/**
	 * 友情链接列表
	 * @author 佛祖<381361943@qq.com>
	 */
	public function friendship()
	{
		$list = M('friendship')->order("id desc")->select();
		$this->assign('list',$list);
		$this->display();
	}
	/**
	 * 友情链接删除
	 * @author 佛祖<381361943@qq.com>
	 */
	public function friendshipdelete()
	{
		$id = I('get.id',0);
		$id == 0?$this->error('参数错误,id不能为0'):1;
		M('friendship')->delete($id);
		$this->success('删除成功',U('friendship'));
	}
	/**
	 * 友情链接修改
	 * @author 佛祖<381361943@qq.com>
	 */
	public function friendshipedit()
	{
		$id = I('id',0);
		if(IS_GET)
		{
			$id = I('get.id',0);
			$id == 0?$this->error('参数错误,id不能为0'):1;
			$list = M('friendship')->find($id);
			!$list?$this->error('数据已经被删除'):1;
			$this->assign('list',$list);
			$this->display();
		}
		else
		{
			$data = I('post.');
			unset($data['id']);
			if(strstr($data['href'],'http://') === false && strstr($data['href'],'https://')===false)
				$this->error('轮播链接请在开头处拼接http:// 或https://');
			M('friendship')->where(array('id'=>$id))->save($data);
			$this->success('修改成功',U('friendship'));
		}	
	}
	/**
	 * 友情链接添加
	 * @author 佛祖<381361943@qq.com>
	 */
	public function friendshipadd()
	{
		/*$a = "http://localhost/douxin/Uploads/Pictures/20180725/5b580cdd40a86.jpeg"; 
		$count=strpos($a,"http://localhost"); 
		$str=substr_replace($a,"",$count,strlen('http://localhost'));
		echo $str;die;
		echo get_domain('http://localhost/douxin/Uploads/Pictures/20180725/5b580cdd40a86.jpeg');die;*/
		if(IS_GET)
		{
			$this->display();
		}
		else
		{
			$data = I('post.');
			if(strpos($data['href'],'http://') === false && strpos($data['href'],'https://')===false)
				$this->error('轮播链接请在开头处拼接http:// 或https://');
			M('friendship')->add($data);
			$this->success('添加成功',U('friendship'));
		}	
	}
	/**
	 * 备案信息
	 * @author 佛祖<381361943@qq.com>
	 */
	public function keep()
	{
		if(IS_GET)
		{
			$name = M('keep')->getField('name');
			$this->assign('name',$name);
			$this->display();
		}
		else
		{
			$name = I('name','');
			$name == '' ?$this->error('请填写备案信息'):'';
			M('keep')->where(array('id'=>1))->save(array('name'=>$name));
			$this->success('修改成功');
		}	
		
	}
	/**
	 * 二维码 底部位子
	 * @author 佛祖<381361943@qq.com>
	 */
	public function code()
	{
		if(IS_GET)
		{
			$list = M('code')->where(array('id'=>1))->find();
			$this->assign('list',$list);
			$this->display();
		}
		else
		{
			$post = I('post.');
			$count=strpos($post['picture'],get_domain($post['picture']));
			$post['picture']=substr_replace($post['picture'],"",$count,strlen(get_domain($post['picture'])));
			M('code')->where(array('id'=>1))->save($post);
			$this->success('修改成功');
		}	
	}
}

?>